<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
	%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%request.setCharacterEncoding("utf-8"); %>
<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商品管理</title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <!--bootstrap.min.css-->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
    <!--font-awesome.css?v=4.4.0-->
    <link href="https://cdn.staticfile.org/font-awesome/4.4.0/css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <!--bootstrap.min.css-->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
    <!--custom.css-->
	<link rel="stylesheet" type="text/css" href="<%=basePath%>css/plugins/iCheck/custom.css"/>
	<!--style.css-->
	<link rel="stylesheet" type="text/css" href="<%=basePath%>css/style.css"/>
	<!--jquery.min.js-->
	<script src="https://cdn.staticfile.org/jquery/2.1.4/jquery.min.js"></script>
</head>
<body class="gray-bg">
<div class="row">
<form action="${pageContext.request.contextPath}/GoodsServlet/listGoodsInfo">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>商品管理</h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                        <a class="close-link">
                            <i class="fa fa-times"></i>
                        </a>
                    </div>
                </div>
                <div class="ibox-content">
					<div class="row">
						<div class="col-sm-2 m-b-xs">
							<select class="form-control" name="gtype_id" id="gtype" style="height:40px;border:1px solid #1AB394">
								<option value="0">包</option>
								<c:forEach items="${goodsTypeList}" var="goodsType">
									<c:if test="${gtype_id==goodsType.gtype_id}">
										<option value="${goodsType.gtype_id}" selected>${goodsType.gtype_name}</option>
									</c:if> 
									<c:if test="${gtype_id!=goodsType.gtype_id}">
										<option value="${goodsType.gtype_id}">${goodsType.gtype_name}</option>
									</c:if> 
								</c:forEach>
							</select>
						</div>
						<div class="col-sm-6">
						<!-- 五个按钮 -->
						<div class="btn btn-primary fa fa-plus" 
							onclick="addGoods()"
							style="margin-left: 15px; border-radius: 0%; width: 80px; height: 40px; text-align: center; line-height: 27px;">&nbsp;新增</div>
						<div class="btn btn-primary fa fa-edit" 
							onclick="editGoods()"
							style="border-radius: 0%; width: 80px; height: 40px; text-align: center; line.-height: 27px;">&nbsp;修改</div>
						<div class="btn btn-primary fa fa-recycle"
							onclick="deleteGoods()"
							style="border-radius: 0%; width: 80px; height: 40px; text-align: center; line-height: 27px;">&nbsp;删除</div>
						<div class="btn btn-primary fa fa-eye"
							onclick="checkGoods()"
							style="border-radius: 0%; width: 80px; height: 40px; text-align: center; line-height: 27px;">&nbsp;查看</div>
						<div class="btn btn-primary fa fa-eye"
							onclick="checkSales()"
							style="border-radius: 0%; width: 180px; height: 40px; text-align: center; line-height: 27px;">&nbsp;查看商品销量走势</div>
						</div>
						
						<div class="col-sm-3 pull-right">
							<div class="input-group">
								<input type="text" name="goods" value="${goods}" placeholder="请输入商品名或商品编号..."
									class="input-sm form-control"> <span
									class="input-group-btn">
									<button type="submit" class="btn btn-sm btn-primary">
										搜索</button>
								</span>
							</div>
						</div>
					</div>
					<div class="table-responsive">
                        <table class="table table-striped table-hover table-bordered dataTables-example">
                            <thead>
                            <tr>
                                <th>选择</th>
                                <th>商品图片</th>
                                <th>商品名称</th>
                                <th>市场价</th>
                                <th>库存</th>
                                <th>状态</th>
                                <th>最后修改时间</th>
                            </tr>
                            </thead>
                            <tbody>
                            <c:forEach items="${goodsInfoList}" var="goods">
	                            <tr>
	                                <td>
	                                    <input type="checkbox" class="i-checks goods" goods_id="${goods.goods_id}" value="${goods.goods_id}" name="goods_id">
	                                </td>
	                                <td><img src="${goods.goods_img}" width="45" height="45" alt="" /></td>
	                                <td>${goods.goods_name}</td>
	                                <td>${goods.goods_price}</td>
	                                <td>${goods.goods_count}</td>
	                                 <c:if test="${goods.state_name=='上架'}">
                                		<td align="center" style="line-height: 34px;"><input type="button" onclick="transitionState(this)" goods_id="${goods.goods_id}" state_id="${goods.state_id}" class="btn btn-info btn-xs" value="${goods.state_name }" /></td>
                                	</c:if>
                               		<c:if test="${goods.state_name=='下架'}">
                                		<td align="center" style="line-height: 34px;"><input type="button" onclick="transitionState(this)" goods_id="${goods.goods_id}" state_id="${goods.state_id}" class="btn btn-danger btn-xs" value="${goods.state_name }" /></td>
                               		</c:if>
	                                <td>${goods.goods_time}</td>
	                            </tr>
                            </c:forEach>
                            </tbody>
                        </table>
                    </div>

                </div>
            </div>
        </div>
        </form>
        <div class="pag text-center"></div>
    </div>
<!--bootstrap.min.js-->
<script src="https://cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<!--jquery.peity.min.js-->
<script src="<%=basePath%>js/plugins/peity/jquery.peity.min.js" type="text/javascript" charset="utf-8"></script>
<!--content.js-->
<script src="<%=basePath%>js/content.js" type="text/javascript" charset="utf-8"></script>
<!--icheck.min.js-->
<script src="<%=basePath%>js/plugins/iCheck/icheck.min.js" type="text/javascript" charset="utf-8"></script>
<!--peity-demo.js-->
<script src="<%=basePath%>js/demo/peity-demo.js" type="text/javascript" charset="utf-8"></script>
<!--layer.min.js-->
<script src="<%=basePath%>js/plugins/layer/layer.min.js" type="text/javascript" charset="utf-8"></script>
<!-- toastr.min.js -->
<script src="<%=basePath%>js/plugins/toastr/toastr.min.js" type="text/javascript" charset="utf-8"></script>
<!-- jquery-bootstrap-pagination.js -->
<script src="<%=basePath%>js/plugins/jquery-bootstrap-pagination/jquery-bootstrap-pagination.js" type="text/javascript" charset="utf-8"></script>
<!-- pubic.js -->
<script src="<%=basePath%>js/pubic.js" type="text/javascript" charset="utf-8"></script>


<script>
$(document).ready(function () {
	//复选框选中
    $(".i-checks").iCheck(
    	{checkboxClass: "icheckbox_square-green", 
    	radioClass: "iradio_square-green",})
});
</script>
	
<script>
	
	//删除商品
	function deleteGoods(){
		if($('.goods:checked').length>1){
			layer.msg('一旦删除无法恢复<br>是否删除',{
    			time:4000,
    			btn: ['确定','取消'],
    			yes:function(){
    				$.get('${pageContext.request.contextPath}/GoodsServlet/deleteSelectedGoods',$('.goods:checked').serialize(),function(data){
    					if(data==1){
    						$('.goods:checked').each(function(){
    							var n = $(this).parent('tr').index();
    							$('table').find('tr:eq('+n+')').remove();
    						});
    						layer.msg('删除成功',{
    	    	    			time:1000
    	    	    		});
    					}else{
    						layer.msg('删除失败',{
    	    	    			time:1000
    	    	    		});
    					}
    				});
    			}
    		});
    	}else if($('.goods:checked').length==0){
    		layer.msg('请选择一条商品信息!!!', {icon: 5});
    	}else{
    		layer.msg('一旦删除无法恢复<br>是否删除',{
    			time:4000,
    			btn: ['确定','取消'],
    			yes:function(){
    				$.get('${pageContext.request.contextPath}/GoodsServlet/deleteGoodsById',{"goods_id":$('.goods:checked').attr('goods_id')},function(data){
    					if(data==1){
    						$('.goods:checked').parent().parent().parent().remove();
    						layer.msg('删除成功',{
    	    	    			time:1000
    	    	    		});
    					}else{
    						layer.msg('删除失败',{
    	    	    			time:1000
    	    	    		});
    					}
    				});
    			}
    		});
    	}
	}
	//新增商品
	function addGoods(){
		layer.open({
    		type: 2,
            skin: 'body .demo-class .layui-layer-title{background:#c00; color:#fff; border: none;}',
            title:'商品新增',
            area:['750px','500px'],
            closeBtn: 0,
            anim: 1,
            shadeClose:true,
            btn:'新增',
            content:['${pageContext.request.contextPath}/pages/houtai/goods/goods_add.jsp','no'],
            yes:function(index,layero){
            	$("iframe").contents().find('#submit').click();
                $("iframe").contents().find('form').submit();
                account = setTimeout(function() {layer.close(index);}, 100);
            }
    	}); 
	}
	//修改商品
	function editGoods(){
		if($('.goods:checked').length>1){
    		layer.msg('只能选择一条商品信息!!!', {icon: 5});
    	}else if($('.goods:checked').length==0){
    		layer.msg('请选择一条商品信息!!!', {icon: 5});
    	}else{
    		layer.open({
        		type: 2,
                skin: 'body .demo-class .layui-layer-title{background:#c00; color:#fff; border: none;}',
                title:'商品信息修改',
                area:['750px','500px'],
                closeBtn: 0,
                anim: 1,
                shadeClose:true,
                btn:'修改',
                content:['${pageContext.request.contextPath}/pages/houtai/goods/goods_edit.jsp?goods_id='+$('.goods:checked').attr('goods_id'),'no'],
                yes:function(index,layero){
                	$("iframe").contents().find('#submit').click();
                	//$("iframe").contents().find('shangchuan').children('a').click();
                    $("iframe").contents().find('form').submit();
                    account = setTimeout(function() {layer.close(index);}, 100);
                }
        	}); 
    	}
	}
	//查看商品
	function checkGoods(){
		if($('.goods:checked').length>1){
    		layer.msg('只能选择一条商品信息!!!', {icon: 5});
    	}else if($('.goods:checked').length==0){
    		layer.msg('请选择一条商品信息!!!', {icon: 5});
    	}else{
    		layer.open({
	    		type: 2,
	            skin: 'body .demo-class .layui-layer-title{background:#c00; color:#fff; border: none;}',
	            title:'商品查看',
	            area:['800px','550px'],
	            closeBtn: 0,
	            shadeClose:true,
	            btn:'返回',
	            content:['${pageContext.request.contextPath}/pages/houtai/goods/goods_look.jsp?goods_id='+$('.goods:checked').val(),'no'],
	            yes:function(index,layero){
	               layer.close(index);
	            }
	    	});
    	}
	
	}
	//查看商品销量走势
	function checkSales(){
		layer.open({
    		type: 2,
            skin: 'body .demo-class .layui-layer-title{background:#c00; color:#fff; border: none;}',
            title:'商品销量走势',
            btn:'返回',
            area:['1250px','600px'],
            closeBtn: 0,
            shadeClose:true,
            content:['${pageContext.request.contextPath}/pages/houtai/goods/goods_sales.jsp','no'],
            yes:function(index,layero){
               layer.close(index);
            }
    	});
	}
	//商品的上架与下架
	function transitionState(obj){
		if($(obj).val()=='上架'){
			layer.msg('是否下架',{
				time:4000,
				btn:['是','否'],
				yes:function(index){
					$.get('${pageContext.request.contextPath}/GoodsServlet/editGoodsState',{"goods_id":$(obj).attr('goods_id'),"state_id":$(obj).attr('state_id')},function(data){
						if(data==1){
							$(obj).removeClass('btn-info').addClass('btn-danger');
							$(obj).val('下架');
							layer.msg('下架成功',{
								time:1000
							});
						}else{
							layer.msg('下架失败',{
								time:1000
							});
						}
					});
					
				}
			});
		}else{
			layer.msg('是否上架',{
				time:4000,
				btn:['是','否'],
				yes:function(index){
					$.get('${pageContext.request.contextPath}/GoodsServlet/editGoodsState',{"goods_id":$(obj).attr('goods_id'),"state_id":$(obj).attr('state_id')},function(data){
						if(data==1){
							$(obj).removeClass('btn-danger').addClass('btn-info');
							$(obj).val('上架');
							layer.msg('上架成功',{
								time:1000
							});
						}else{
							layer.msg('上架失败',{
								time:1000
							});
						}
					});
				}
			});
		}
	}
	//select改变状态提交表单
	$('#gtype').change(function(){
		$('form').submit();
	});
	//分页
	pagination(${pageCount},${pageNum});
	
</script>
</body>
</html>